/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.finance.mq;


import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.medusa.gruul.common.data.tenant.ShopContextHolder;
import com.medusa.gruul.common.data.tenant.TenantContextHolder;
import com.medusa.gruul.finance.api.constant.FinanceQueueNameConstant;
import com.medusa.gruul.finance.service.*;
import com.medusa.gruul.order.api.model.DataInitMessage;
import com.medusa.gruul.order.api.model.OrderVo;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;


/**
 * @Description: 订阅订单消息demo
 * @Author: alan
 * @Date: 2019/10/6 13:49
 */
@Slf4j
@Component
public class FinanceListener {

    @Resource
    private IFinanceTradeDetailService financeTradeDetailService;


    /**
     * 使用方法：
     * 1.创建自己队列 assemble.order.payed  模块名.业务名.动作
     * 2.通过路由键 gruul.order.payed 到交换机 gruul.order.direct 绑定自己创建的队列
     * 3.收到消息之后手动确认，不然消息会重复发送
     *
     * @param orderVo
     * @param properties
     * @param channel
     * @return void
     * @author alan
     * @date 2019/12/1 11:07
     */
    @RabbitListener(queues = FinanceQueueNameConstant.ORDER_PAYED)
    public void payed(OrderVo orderVo, MessageProperties properties, Channel channel) throws IOException {
        log.info("payed receive message:" + orderVo.toString());
        //插入交易流水记录
        TenantContextHolder.setTenantId(orderVo.getTenantId());
        ShopContextHolder.setShopId(orderVo.getShopId());
        //插入交易记录和商品交易明细
        financeTradeDetailService.addTrade(orderVo);
        //手动确认
        channel.basicAck(properties.getDeliveryTag(), true);

    }

    /**
     * 将商品的返利纳入账单的支出
     * @param orderVo 商品
     * @param properties properties
     * @param channel channel;
     */
    @RabbitListener(queues = FinanceQueueNameConstant.ORDER_COMPLETED)
    public void orderCompleted(OrderVo orderVo,MessageProperties properties, Channel channel) throws IOException {
        financeTradeDetailService.addRebateDetail(orderVo);
        channel.basicAck(properties.getDeliveryTag(), true);
    }

    /**
     * 订单退款消息，订单退款记录
     *
     * @param orderVo
     * @param properties
     * @param channel
     * @return void
     * @author alan
     * @date 2021/3/17 21:57
     */
    @RabbitListener(queues = FinanceQueueNameConstant.ORDER_RETURN)
    public void orderReturn(OrderVo orderVo, MessageProperties properties, Channel channel) throws IOException {
        log.info("return  message:" + orderVo.toString());
        //插入交易流水记录
        TenantContextHolder.setTenantId(orderVo.getTenantId());
        ShopContextHolder.setShopId(orderVo.getShopId());
        Boolean success = financeTradeDetailService.addReturn(orderVo);
        if (success) {
            //手动确认
            channel.basicAck(properties.getDeliveryTag(), true);
        }
    }

}

